package za.org.foobar.famon.appengine.migration;

import com.google.appengine.api.datastore.Entity;

import java.io.PrintWriter;

/**
 *
 */
public class DataMigrationAddField implements DataMigration {
    private Class cls;
    private String field;
    private Object defaultValue;

    public DataMigrationAddField(Class cls, String field, Object defaultValue) {
        this.cls = cls;
        this.field = field;
        this.defaultValue = defaultValue;
    }

    public Class getEntityClass() {
        return cls;
    }

    public boolean migrate(Entity e, PrintWriter out) {
        if (!e.hasProperty(field)) {
            e.setProperty(field, defaultValue);
            out.println("Added " + field + " = " + defaultValue + " to " + cls.getSimpleName() + " " + e.getKey());
            return true;
        }
        return false;
    }

    public String toString() {
        return "DataMigrationAddField{" +
                "cls=" + cls.getSimpleName() +
                ", field='" + field + '\'' +
                ", defaultValue=" + defaultValue +
                '}';
    }
}
